﻿using System;
using System.Collections;
using System.Data;
using DotNetNuke.Services.Search;
using DotNetNuke.Common.Utilities;
using gdlmo.DNN.Modules.Informations.Data;

namespace gdlmo.DNN.Modules.Informations.Business
{
	public class InformationsController : DotNetNuke.Entities.Modules.ISearchable, DotNetNuke.Entities.Modules.IPortable
	{
		#region "Public Methods"		
		public InformationsInfo Get(int InfoId,int moduleId )
		{
			return (InformationsInfo)DotNetNuke.Common.Utilities.CBO.FillObject(DataProvider.Instance().GetInformations(InfoId,moduleId), typeof(InformationsInfo));
		}

		public ArrayList List()
		{
			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().ListInformations(), typeof(InformationsInfo));
		}

	
		public int Add(InformationsInfo objInformations)
		{
			return (int)DataProvider.Instance().AddInformations(objInformations.ModuleId, objInformations.Title, objInformations.URL, objInformations.Description, objInformations.ViewOrder, objInformations.CreateDate, objInformations.ExpireDate, objInformations.Publisher, objInformations.DeptName, objInformations.PublishDate,objInformations.CreatedByUser);
		}
	
		public void Update(InformationsInfo objInformations)
		{
			DataProvider.Instance().UpdateInformations(objInformations.InfoId, objInformations.ModuleId, objInformations.Title, objInformations.URL, objInformations.Description, objInformations.ViewOrder, objInformations.CreateDate, objInformations.ExpireDate, objInformations.Publisher, objInformations.DeptName, objInformations.PublishDate,objInformations.CreatedByUser);
		}
		
		public void Delete(int InfoId)
		{
			DataProvider.Instance().DeleteInformations(InfoId);
		}
		public ArrayList GetByModule(int moduleId)
		{
			return DotNetNuke.Common.Utilities.CBO.FillCollection(DataProvider.Instance().GetByModule(moduleId), typeof(InformationsInfo));
		}
		/// <summary>
		/// 取分页数据
		/// </summary>
		/// <param name="PageIndex">页序</param>
		/// <param name="PageSize">每页记录数</param>
		/// <param name="PageCount">可返回的pagecount</param>
		/// <returns>记录集（arraylist）</returns>
		/// <remarks>
		/// 2008.3.5 gdlmo modify this function :一次调用存储过程返回pagecount与结果集
		/// </remarks>
		/// <example>
		/// <code>
		/// int pcount=0;
		/// ArrayList list = objCtlInfo.GetPagingData(0,10,ref pcount);
		/// </code>
		/// </example>
		public ArrayList GetPagingData(int PageIndex,int PageSize,ref int PageCount)
		{			
			ArrayList list = new ArrayList();
			IDataReader dr = DataProvider.Instance().GetPageData(PageIndex,PageSize);
			dr.Read();
			int Rcount = Convert.ToInt32(dr[0]);			
			dr.NextResult();
			list = DotNetNuke.Common.Utilities.CBO.FillCollection(dr, typeof(InformationsInfo));
			
			if ((Rcount % PageSize) > 0)
			{
				PageCount = (Rcount / PageSize) + 1;
			}
			else
			{
				PageCount = Rcount / PageSize;
			}	
			return list;
			
		}
		public InformationsInfo GetById(int InfoId)
		{
			return (InformationsInfo)DotNetNuke.Common.Utilities.CBO.FillObject(DataProvider.Instance().GetById(InfoId), typeof(InformationsInfo));
		}
		#endregion

		#region "Optional Interfaces"		
		public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(DotNetNuke.Entities.Modules.ModuleInfo ModInfo)
		{
			DotNetNuke.Services.Search.SearchItemInfoCollection SearchItemCollection = new DotNetNuke.Services.Search.SearchItemInfoCollection();
			ArrayList Informations = this.GetByModule(ModInfo.ModuleID);
			foreach(InformationsInfo info in Informations)
			{
				int UserId ;
				if (gdlmo.Common.StringHelper.IsNum(info.CreatedByUser))
				{
					UserId = Convert.ToInt32(info.CreatedByUser);
				}
				else
				{
					UserId = 2;
				}
				string strContent = HtmlUtils.Shorten(HtmlUtils.Clean(System.Web.HttpUtility.HtmlDecode(info.Title),false),100,"...");
				string strDescription = HtmlUtils.Shorten(HtmlUtils.Clean(System.Web.HttpUtility.HtmlDecode(info.Description),false),100,"...");
				DotNetNuke.Services.Search.SearchItemInfo SearchItem = new DotNetNuke.Services.Search.SearchItemInfo(ModInfo.ModuleTitle + "-" + info.Title,
					strDescription,UserId,info.CreateDate,ModInfo.ModuleID,info.InfoId.ToString(),strContent,"mid=" + ModInfo.ModuleID.ToString() + "&ctl=SearchItemCollection&InfoId=" + info.InfoId.ToString(), DotNetNuke.Common.Utilities.Null.NullInteger);
				
                    SearchItemCollection.Add(SearchItem);
			}
			
			return SearchItemCollection;
		}
	
		public string ExportModule(int ModuleID)
		{
			return "";
		}

		public void ImportModule(int ModuleID, string Content, string Version, int UserId)
		{
		}
		#endregion
	}
}
